// Copyright (c) 2010, SMB SAAS Systems Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// - Redistributions of source code must  retain  the  above copyright notice, this
//   list of conditions and the following disclaimer.
//
// - Redistributions in binary form  must  reproduce the  above  copyright  notice,
//   this list of conditions  and  the  following  disclaimer in  the documentation
//   and/or other materials provided with the distribution.
//
// - Neither  the  name  of  the  SMB SAAS Systems Inc.  nor   the   names  of  its
//   contributors may be used to endorse or  promote  products  derived  from  this
//   software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,  BUT  NOT  LIMITED TO, THE IMPLIED
// WARRANTIES  OF  MERCHANTABILITY   AND  FITNESS  FOR  A  PARTICULAR  PURPOSE  ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL,  SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO,  PROCUREMENT  OF  SUBSTITUTE  GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  HOWEVER  CAUSED AND ON
// ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT  LIABILITY,  OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING  IN  ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

using System;
using System.Web;
using dotPanel.EnterpriseServer;


namespace dotPanel.Portal
{
	public partial class Login : dotPanelModuleBase
	{
		string ipAddress;

		private string RedirectUrl
		{
			get
			{
				string redirectUrl = "";
				if (Request["returnurl"] != null)
				{
					// return to the url passed to signin
					redirectUrl = HttpUtility.UrlDecode(Request["returnurl"]);
				}
				else
				{
					redirectUrl = PortalUtils.LoginRedirectUrl;
				}
				return redirectUrl;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindControls();
			}

			// capture Enter key
			//DotNetNuke.UI.Utilities.ClientAPI.RegisterKeyCapture(this.Parent, btnLogin, 13);

			// get user IP
			if (Request.UserHostAddress != null)
				ipAddress = Request.UserHostAddress;

			// update password control
			txtPassword.Attributes["value"] = txtPassword.Text;

			// autologin
			string usr = Request["u"];
			if (String.IsNullOrEmpty(usr))
				usr = Request["user"];

			string psw = Request["p"];
			if (String.IsNullOrEmpty(psw))
				psw = Request["pwd"];
			if (String.IsNullOrEmpty(psw))
				psw = Request["password"];

			if (!String.IsNullOrEmpty(usr) && !String.IsNullOrEmpty(psw))
			{
				// perform login
				LoginUser(usr, psw, false, String.Empty, String.Empty);
			}
		}

		private void BindControls()
		{
			// load languages
			PortalUtils.LoadCultureDropDownList(ddlLanguage);            

			// load themes
			PortalUtils.LoadThemesDropDownList(ddlTheme);

			// try to get the last login name from cookie
			HttpCookie cookie = Request.Cookies["dotPanelLogin"];
			if (cookie != null)
			{
				txtUsername.Text = cookie.Value;
			}
		}

		protected void cmdForgotPassword_Click(object sender, EventArgs e)
		{
			Response.Redirect(EditUrl("forgot_password"), true);
		}

		protected void btnLogin_Click(object sender, EventArgs e)
		{
			// validate input
			if (!Page.IsValid)
				return;

			// perform login
			LoginUser(txtUsername.Text.Trim(), txtPassword.Text, chkRemember.Checked,
				ddlLanguage.SelectedValue, ddlTheme.SelectedValue);
		}

		private void LoginUser(string username, string password, bool rememberLogin,
			string preferredLocale, string theme)
		{
			// status
			int loginStatus = PortalUtils.AuthenticateUser(username, password, ipAddress,
				rememberLogin, preferredLocale, theme);

			if (loginStatus < 0)
			{
                ShowWarningMessage("WrongLogin");
			}
			else
			{
                // redirect by shortcut
                ShortcutRedirect();

                // standard redirect
				Response.Redirect(RedirectUrl, true);
			}
		}

        private void ShortcutRedirect()
        {
            if (PanelSecurity.EffectiveUser.Role == UserRole.Administrator)
                return; // not for administrators

            string shortcut = Request["shortcut"];
            if ("vps".Equals(shortcut, StringComparison.InvariantCultureIgnoreCase))
            {
                // load hosting spaces
                PackageInfo[] packages = ES.Services.Packages.GetMyPackages(PanelSecurity.EffectiveUserId);
                if (packages.Length == 0)
                    return; // no spaces - exit

                // check if some package has VPS resource enabled
                foreach (PackageInfo package in packages)
                {
                    int packageId = package.PackageId;
                    PackageContext cntx = PackagesHelper.GetCachedPackageContext(packageId);
                    if (cntx.Groups.ContainsKey(ResourceGroups.VPS))
                    {
                        // VPS resource found
                        // check created VPS
                        VirtualMachineMetaItemsPaged vms = ES.Services.VPS.GetVirtualMachines(packageId, "", "", "", 0, Int32.MaxValue, false);
                        if (vms.Items.Length == 1)
                        {
                            // one VPS - redirect to its properties screen
                            Response.Redirect(PortalUtils.NavigatePageURL("SpaceVPS", "SpaceID", packageId.ToString(),
                                "ItemID=" + vms.Items[0].ItemID.ToString(), "ctl=vps_general", "moduleDefId=VPS"));
                        }
                        else
                        {
                            // several VPS - redirect to VPS list page
                            Response.Redirect(PortalUtils.NavigatePageURL("SpaceVPS", "SpaceID", packageId.ToString(),
                                "ctl=", "moduleDefId=VPS"));
                        }
                    }
                }

                // no VPS resources found
                // redirect to space home
                if (packages.Length == 1)
                    Response.Redirect(PortalUtils.GetSpaceHomePageUrl(packages[0].PackageId));
            }
        }

		private void SetCurrentLanguage()
		{
            PortalUtils.SetCurrentLanguage(ddlLanguage.SelectedValue);
            Response.Redirect(Request.Url.ToString());
		    
		}
        
        protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
		{
		    SetCurrentLanguage();
        }

		protected void ddlTheme_SelectedIndexChanged(object sender, EventArgs e)
		{
			PortalUtils.SetCurrentTheme(ddlTheme.SelectedValue);
			Response.Redirect(Request.Url.ToString());
		}
	}
}